package utils;


import java.util.Collection;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;

import org.apache.commons.collections.bag.SynchronizedBag;

import basketMatcher.Basket;

public class MultiTreeMap {

	private TreeMap<Double , Vector<Basket> > m_tree;
	
	
	public MultiTreeMap(){
		m_tree = new TreeMap<Double, Vector<Basket>>();
	}
	
	public synchronized void put(Double d  , Basket b ){
		
		Vector<Basket > v = m_tree.get(d);
		if (  v == null) {
			v = new Vector<Basket>();
			v.add(b);
			m_tree.put(d, v);
		}
		else 
			v.add(b);
	}
	
	public synchronized Vector<Basket> get(Double d   ){
		return m_tree.get(d);
	}
	
//	public synchronized Vector<Basket> remove(Double d ){
//		return m_tree.remove(d);
//	}
	
	public synchronized void removeBasket(double d , Basket b ) {
		Vector<Basket> v = m_tree.get(d);
			 v.remove(b);
			 System.out.println(b);
			 System.out.println(v.contains(b));
		if (v.isEmpty()){
			m_tree.remove(d);
		}
		
	}
	
	
	
	public synchronized Set<Double> keySet() {
		return m_tree.keySet();
	}

	public synchronized Collection<Vector<Basket>> values() {
		return m_tree.values();
	}

	public int  size() {
		return m_tree.size();
	}
	
	public Double lastKey() {
		return m_tree.lastKey();
	}
	
	public Double firstKey() {
		return m_tree.firstKey();
	}
	public Double higherEntry(Double key){
		
		if (m_tree.higherEntry(key) != null ) 
			return m_tree.higherEntry(key).getKey();
		else
				return key + 1;
	}

	public Double nextKeyInTree(Double key) {
		Double d = higherEntry(key);
		if (d != null)
			return d;
		else
			return key + 1;
	}
	
	public boolean isEmpty() {
		return m_tree.isEmpty();
	}

	public Basket highestBasket() {
		Vector<Basket> v = m_tree.lastEntry().getValue();
		return v.firstElement();
	}

	public void remove(Double current_traversed_key) {
		m_tree.remove(current_traversed_key);
	}

	public void putVector(Double current_traversed_key, Vector<Basket> newv) {
		if(!newv.isEmpty())
			m_tree.put(current_traversed_key, newv);
		
	}
}
